package com.red.controller.common;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.red.constant.MessageConstant;
import com.red.entity.Category;
import com.red.entity.Products;
import com.red.mapper.CategoryMapper;
import com.red.mapper.ProductsMapper;
import com.red.properties.AliOssProperties;
import com.red.result.Result;
import com.red.utils.AliOssUtil;
import com.red.vo.ProductVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author 刘英豪
 */
@RestController
@RequestMapping("/common")
@Slf4j
@CrossOrigin
public class common {

//    @Autowired
//    AliOssUtil aliOssUtil;
    @Autowired
    private AliOssProperties aliOssProperties;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private ProductsMapper productsMapper;

    @PostMapping("/upload")
    @ApiOperation("文件上传")
    public Result<String> upload(MultipartFile file) {
        log.info("文件上传：{}", file);

        try {
            AliOssUtil aliOssUtil = new AliOssUtil(aliOssProperties.getEndpoint(), aliOssProperties.getAccessKeyId(), aliOssProperties.getAccessKeySecret(), aliOssProperties.getBucketName());
            //获取原始文件名
            String originalFilename = file.getOriginalFilename();
            //截取文件名字获取后缀
            String extension = originalFilename.substring(originalFilename.lastIndexOf('.'));
            //构造新文件名字
            String objectName = UUID.randomUUID().toString() + extension;
            String filePath = aliOssUtil.upload(file.getBytes(), objectName);
            return Result.success(filePath);
        } catch (IOException e) {
            log.info("文件上传失败:{}", e);
        }

        return Result.error(MessageConstant.UPLOAD_FAILED);
    }

    /**
     * 查询所有分类
     * @return
     */
    @GetMapping("/selectCategory")
    public Result<List<Category>> selectCategory(){
        List<Category> list = categoryMapper.selectList(new QueryWrapper<>());
        return Result.success(list);
    }

    /**
     * 根据分类ID查询商品信息
     * @param id
     * @return
     */
    @GetMapping("/selectProductById/{id}")
    public Result<List<ProductVO>> selectProductById(@PathVariable Long id){
        List<ProductVO> list = new ArrayList<>();
        QueryWrapper<Products> qw = new QueryWrapper<>();
        qw.eq("category_id",id);
        List<Products> products = productsMapper.selectList(qw);
        for (Products product : products) {
            ProductVO productVO = new ProductVO();
            BeanUtils.copyProperties(product,productVO);
            Category category = categoryMapper.selectById(product.getCategoryId());
            productVO.setCategoryName(category.getName());
        }
        return Result.success(list);
    }

}
